package com.gaolang.web.controller;

import com.gaolang.web.entities.User;
import com.gaolang.web.response.Result;
import com.gaolang.web.service.IUserService;
import com.gaolang.web.vo.UserLogin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @author: 小何
 * @description: 登录
 * @date: 2021-02-25 14:47
 */
@RestController
@Api(value = "登录模块",tags = "登录接口")
public class LoginController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户登录",notes = "登录后返回token")
    @PostMapping("/login")
    public Result login(@RequestBody UserLogin userLogin, HttpServletRequest httpServletRequest) {
        return userService.login(userLogin,httpServletRequest);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping("/getInfo")
    public Result getUserInfo(Principal principal) {
        if (null == principal){
            return Result.error();
        }

        String username = principal.getName();

        User user = userService.getUserByUsername(username);
        user.setPassword(null);
        return Result.ok().data("user",user);
    }


    @ApiOperation(value = "退出登录",notes = "用户退出登录")
    @PostMapping("/logout")
    public Result logout() {
        //前端删除token令牌
        return Result.ok();
    }
}   